package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.UnitMemberRepositoryService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.unitMember.UnitMemberAddRequest;
import com.ktwlsoft.archivemanage.payload.unitMember.UnitMemberUpdateRequest;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/unitMember")
public class UnitMemberController {
    @Autowired
    private UnitMemberRepositoryService unitMemberRepositoryService;

    @ApiOperation(value = "查询立档单位人员信息")
    @ResponseBody
    @RequestMapping(value = "/findOne", produces = "application/json", method = RequestMethod.GET)
    public ResponseEntity<?> findUnitMemberInfo(@ApiParam(name = "unitId", value = "立档单位id")
                                                @RequestParam Long unitId) {
        Result result = unitMemberRepositoryService.findUnitMemberInfoById(unitId);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "查询所有立档单位人员列表")
    @ResponseBody
    @RequestMapping(value = "/findList", produces = "application/json", method = RequestMethod.GET)
    public ResponseEntity<?> findUnitMemberList() {
        Result result = unitMemberRepositoryService.findUnitMemberList();
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "新增立档单位人员信息")
    @ResponseBody
    @RequestMapping(value = "/addOne", produces = "application/json", method = RequestMethod.POST)
    public ResponseEntity<?> addUnitMemberInfo(@RequestBody UnitMemberAddRequest request,
                                               BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = unitMemberRepositoryService.addUnitMemberInfo(request);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "新增立档单位人员信息")
    @ResponseBody
    @RequestMapping(value = "/updateOne", produces = "application/json", method = RequestMethod.POST)
    public ResponseEntity<?> updateUnitMemberInfo(@RequestBody UnitMemberUpdateRequest request,
                                                  BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = unitMemberRepositoryService.updateUintMemberInfo(request);
        return ResponseEntity.ok(result);
    }
}
